import Vue from 'vue'
import VueRouter from 'vue-router'
import { getRequest } from '../api/http'

Vue.use(VueRouter)

const routes = [
  {
    path: '/log',
    component: () => import('../views/login.vue'),
    children: [{
      path: '/login',
      name: 'login',
      component: () => import('../components/login.vue')
    }, {
      path: '/register',
      name: 'register',
      component: () => import('../components/register.vue')
    }, {
      path: '/forgetpasw',
      name: 'forgetpasw',
      component: () => import('../components/forgetpasw.vue')
    }]
  },
  {
    path: '/',
    component: () => import('../views/common.vue'),
    children: [
      {
        name: "首页",
        path: '/',
        component: () => import('../views/index.vue')
      },
      {
        name: '商家基本信息',
        path: '/merchantIndex',
        component: () => import('../views/merchant/infomation.vue')
      },
      {
        name: '财务管理',
        path: '/adminlist',
        component: () => import('../views/finance/adminlist.vue')
      },
      {
        name: '申请提现',
        path: '/apply',
        component: () => import('../views/finance/apply.vue')
      },
      {
        name: '提现记录',
        path: '/withdraw',
        component: () => import('../views/finance/withdraw.vue')
      },
      {
        name: '订单管理',
        path: "/order",
        component: () => import('../views/order/orderIndex.vue')
      },
      {
        name: '需求管理',
        path: "/demand",
        component: () => import('../views/demand/demandIndex.vue')
      },
      {
        name: '商品列表',
        path: "/comlist",
        component: () => import('../views/commodity/comlist.vue')
      },
      {
        name: '服务商品',
        path: "/comserver",
        component: () => import('../views/commodity/comserver.vue')
      },
      {
        name: '评价列表',
        path: "/evaluate",
        component: () => import('../views/commodity/evaluate.vue')
      },
      {
        name: '商品类型',
        path: "/comtype",
        component: () => import('../views/commodity/comtype.vue')
      },
      {
        name: '商品规格',
        path: "/comspec",
        component: () => import('../views/commodity/comspec.vue')
      },
      {
        name: '发票管理',
        path: "/invoice",
        component: () => import('../views/invoice/invoiceIndex.vue')
      },
      {
        name: '分类统计',
        path: "/saleClassify",
        component: () => import('../views/sales/saleClassify.vue')
      },
      {
        name: '举报列表',
        path: "/report",
        component: () => import('../views/report/report.vue')
      },
      {
        name: "消息列表",
        path: "/notice",
        component: () => import('../views/notice.vue')
      },
      {
        name: "个人中心",
        path: "/personal",
        component: () => import('../views/personal.vue')
      },
      {
        name: "角色管理",
        path: "/management",
        component: () => import('../views/management/management.vue')
      },
      {
        name: "店员列表",
        path: "/userlist",
        component: () => import('../views/management/userlist.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
function beforelink() {
  if (document.getElementsByClassName('el-main')[0]) {
    document.getElementsByClassName('el-main')[0].scrollTop = 0
  }
  document.body.scrollTop = 0
  document.documentElement.scrollTop = 0
  window.pageYOffset = 0
}
// router.beforeEach((to, from, next) => {
//   if (to.path == '/login' || to.path == '/forgetpasw') {
//     // beforelink()
//     return next()
//   } else {
//     if (sessionStorage.getItem("token") && sessionStorage.getItem("token") != 'undefined') {
//       getRequest('/getRouters', {}).then((res) => {
//         if (res.code == 200) {
//           var arr = res.data
//           // console.log(arr);
//           for (var i = 0; i < arr.length; i++) {
//             if (arr[i].children && arr[i].children.length > 0) {
//               var arr1 = arr[i].children
//               for (var j = 0; j < arr1.length; j++) {
//                 if (arr1[j].path == to.path) {
//                   return next()
//                 }
//               }
//             } else {
//               if (arr[i].path == to.path) {
//                 return next()
//               }
//             }
//           }
//         }
//         return next('')
//       })
//     } else {
//       return next('/login')
//     }
//   }
// })
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
